package com.ilop.sthome.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.aliyun.iot.aep.sdk.apiclient.callback.IoTResponse;
import com.example.common.base.OnCallBackToEdit;
import com.ilop.sthome.mvp.model.ShareModel;
import com.ilop.sthome.mvp.model.common.ShareModelImpl;
import com.example.common.mvp.onModelCallBack;
import com.siterwell.familywellplus.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @date 2020-03-21.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：输入分享人的弹出框
 */
public class SharerDialog extends Dialog {

    @BindView(R.id.et_input)
    EditText etInput;
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_tip)
    TextView tvTip;
    @BindView(R.id.tv_error)
    TextView tvError;
    @BindView(R.id.tv_dialog_confirm)
    Button tvDialogConfirm;

    private Context mContext;
    private String mTitle;
    private String mMsg;
    private String mHint;
    private String mConfirm;
    private OnCallBackToEdit mCallBack;

    private ShareModelImpl mModel;
    private Handler mHandler;

    public SharerDialog(Context context, OnCallBackToEdit callBack) {
        super(context, R.style.window_background2);
        this.mCallBack = callBack;
        this.mContext = context;
        mHandler = new Handler();
        mModel = new ShareModel();
    }

    public void setTitle(String title) {
        this.mTitle = title;
    }

    public void setMsg(String mMsg) {
        this.mMsg = mMsg;
    }

    public void setTitleAndButton(String title, String confirm) {
        this.mTitle = title;
        this.mConfirm = confirm;
    }

    public void setEditHint(String hint) {
        this.mHint = hint;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_edit);
        setCanceledOnTouchOutside(false);
        ButterKnife.bind(this);
        if (mMsg != null){
            tvTip.setVisibility(View.VISIBLE);
            tvTip.setText(mMsg);
        }
        if (mTitle!=null) tvTitle.setText(mTitle);
        if (mHint!=null) etInput.setHint(mHint);
        if (mConfirm!=null) tvDialogConfirm.setText(mConfirm);
        tvDialogConfirm.setEnabled(false);

        ivClose.setOnClickListener(v -> {
            mCallBack.onCancel();
            dismiss();
        });

        etInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (Patterns.EMAIL_ADDRESS.matcher(s).matches() || Patterns.PHONE.matcher(s).matches()){
                    tvDialogConfirm.setEnabled(true);
                }else {
                    tvError.setVisibility(View.GONE);
                    tvDialogConfirm.setEnabled(false);
                }
            }
        });

        tvDialogConfirm.setOnClickListener(v -> {
            String name = etInput.getText().toString();
            String type = checkInputText(name);
            if (type != null){
                mModel.registerCheck(type, name, new onModelCallBack() {
                    @Override
                    public void onResponse(IoTResponse response) {
                        if (response.getCode() == 200){
                            if ((Boolean)response.getData()){
                                mCallBack.onConfirm(name);
                                dismiss();
                            }else {
                                mHandler.post(()->{
                                    tvError.setVisibility(View.VISIBLE);
                                    tvError.setText(response.getLocalizedMsg());
                                });
                            }
                        }
                    }

                    @Override
                    public void onFailure(Exception e) {

                    }
                });
            }
        });
    }

    /**
     * 根据输入框输入的判断是手机还是邮箱地址
     * @param name 输入
     */
    private String checkInputText(String name) {
        if (Patterns.EMAIL_ADDRESS.matcher(name).matches()){
            return "email";
        }else if (Patterns.PHONE.matcher(name).matches()){
            return "phone";
        }else {
            tvError.setVisibility(View.VISIBLE);
            tvError.setText(mContext.getString(R.string.incorrect_format));
            return null;
        }
    }
}
